using Net35.Xrm.Sdk.Discovery;
using System;
namespace Net35.Xrm.Sdk.Client
{
	public class DiscoveryServiceProxy : ServiceProxy<IDiscoveryService>, IDiscoveryService
	{
		internal DiscoveryServiceProxy()
		{
		}
		public DiscoveryServiceProxy(System.Uri uri, System.Uri homeRealmUri, System.ServiceModel.Description.ClientCredentials clientCredentials, System.ServiceModel.Description.ClientCredentials deviceCredentials) : base(uri, homeRealmUri, clientCredentials, deviceCredentials)
		{
		}
		public DiscoveryServiceProxy(IServiceConfiguration<IDiscoveryService> serviceConfiguration, SecurityTokenResponse securityTokenResponse) : base(serviceConfiguration, securityTokenResponse)
		{
		}
		public DiscoveryServiceProxy(IServiceConfiguration<IDiscoveryService> serviceConfiguration, System.ServiceModel.Description.ClientCredentials clientCredentials) : base(serviceConfiguration, clientCredentials)
		{
		}
		public DiscoveryServiceProxy(IServiceManagement<IDiscoveryService> serviceManagement, SecurityTokenResponse securityTokenResponse) : this(serviceManagement as IServiceConfiguration<IDiscoveryService>, securityTokenResponse)
		{
		}
		public DiscoveryServiceProxy(IServiceManagement<IDiscoveryService> serviceManagement, System.ServiceModel.Description.ClientCredentials clientCredentials) : this(serviceManagement as IServiceConfiguration<IDiscoveryService>, clientCredentials)
		{
		}
		public DiscoveryResponse Execute(DiscoveryRequest request)
		{
			bool? retry = null;
			do
			{
				bool forceClose = false;
				try
				{
					using (new DiscoveryServiceContextInitializer(this))
					{
						return base.ServiceChannel.Channel.Execute(request);
					}
				}
				catch (System.ServiceModel.Security.MessageSecurityException messageSecurityException)
				{
					forceClose = true;
					retry = base.ShouldRetry(messageSecurityException, retry);
					if (!retry.GetValueOrDefault())
					{
						throw;
					}
				}
				catch (System.ServiceModel.EndpointNotFoundException)
				{
					forceClose = true;
					retry = new bool?(base.HandleFailover(retry));
					if (!retry.GetValueOrDefault())
					{
						throw;
					}
				}
				catch (TimeoutException)
				{
					forceClose = true;
					retry = new bool?(base.HandleFailover(retry));
					if (!retry.GetValueOrDefault())
					{
						throw;
					}
				}
				catch (System.ServiceModel.FaultException<DiscoveryServiceFault> faultException)
				{
					forceClose = true;
					retry = base.HandleFailover(faultException.Detail, retry);
					if (!retry.GetValueOrDefault())
					{
						throw;
					}
				}
				catch
				{
					forceClose = true;
					throw;
				}
				finally
				{
					this.CloseChannel(forceClose);
				}
			}
			while (retry.HasValue && retry.Value);
			return null;
		}
	}
}
